//
//  BrowsingHistoryController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/25.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "BrowsingHistoryController.h"
#import "PersonalHomePageCell.h"
#import "PersonalHomePageModel.h"
#import "PersonalHomePageArticleCell.h"
#import "PersonalHomePageVideoCell.h"
#import "PersonalHomePageQuestionAnswerCell.h"
#import "PersonalHomePageFoodCell.h"

@interface BrowsingHistoryController ()<UITableViewDelegate,UITableViewDataSource>
/**listView*/
@property (nonatomic,strong)UITableView*listView;
@property (nonatomic , strong)NSMutableArray *dataArray;

/**rightBtn*/
@property (nonatomic,strong)UIButton*righBtn;

/**底部视图*/
@property (nonatomic,strong)UIView*bottomView;

@end

@implementation BrowsingHistoryController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"历史记录";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.righBtn];
    
    [self.view addSubview:self.listView];
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.bottomView = [self createBottomView];
    self.bottomView.hidden = YES;
    [self.view addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(40);
    }];

}
-(void)rightAction:(UIButton *)button{
    self.righBtn.selected = !button.selected;
    [self.listView reloadData];
    self.bottomView.hidden = !self.righBtn.selected;
    CGFloat botton = 0;
    if (self.righBtn.selected) {
        botton = -40;
    }
    [self.listView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(botton);
    }];
}

#pragma mark --UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonalHomePageCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageCell class]) forIndexPath:indexPath];
    PersonalHomePageArticleCell * cell1 = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageArticleCell class]) forIndexPath:indexPath];
    PersonalHomePageVideoCell * cell2 = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageVideoCell class]) forIndexPath:indexPath];
    PersonalHomePageQuestionAnswerCell * cell4 = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonalHomePageQuestionAnswerCell class]) forIndexPath:indexPath];

    PersonalHomePageModel * model = self.dataArray[indexPath.row];
    model.isShare = YES;
    kWeakSelf(weakSelf);
    cell.selecteAction = ^(BOOL selecte) {
        DLog(@"%@",indexPath);
        [weakSelf uploadModel:indexPath select:selecte];
    };
    cell1.selecteAction = ^(BOOL selecte) {
        DLog(@"%@",indexPath);
        [weakSelf uploadModel:indexPath select:selecte];
    };
    cell2.selecteAction = ^(BOOL selecte) {
        DLog(@"%@",indexPath);
        [weakSelf uploadModel:indexPath select:selecte];
    };
    cell4.selecteAction = ^(BOOL selecte) {
        DLog(@"%@",indexPath);
        [weakSelf uploadModel:indexPath select:selecte];
    };
    if (model.type == PersonalHomePageArticle) {
        [cell1 reloadCell:self.righBtn.selected];
        cell1.model = model;
        return cell1;
    }
    if (model.type == PersonalHomePageVideo) {
        cell2.model = model;
        [cell2 reloadCell:self.righBtn.selected];
        return cell2;
    }
    if (model.type == PersonalHomePageQanswer) {
        cell4.model = model;
        [cell4 reloadCell:self.righBtn.selected];
        return cell4;
    }
    [cell reloadCell:self.righBtn.selected];
    cell.model = model;
    return cell;
}
-(void)uploadModel:(NSIndexPath *)indepath select:(BOOL)select{
    PersonalHomePageModel * modelItem = self.dataArray[indepath.section];
    modelItem.isSelecte = select;
    self.dataArray[indepath.row] = modelItem;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.navigationController pushViewController:[ImageAndTextDetailController new] animated:YES];
}
-(UITableView*)listView{
    if(!_listView){
        _listView=[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 44.0f;
        _listView.separatorColor = SeparatorColor;
        _listView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);

        _listView.tableFooterView = UIView.new;
        [_listView registerClass:[PersonalHomePageCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageCell class])];
        [_listView registerClass:[PersonalHomePageArticleCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageArticleCell class])];
        [_listView registerClass:[PersonalHomePageVideoCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageVideoCell class])];
        [_listView registerClass:[PersonalHomePageQuestionAnswerCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageQuestionAnswerCell class])];
        [_listView registerClass:[PersonalHomePageFoodCell class] forCellReuseIdentifier:NSStringFromClass([PersonalHomePageFoodCell class])];

    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PersonalHomePage" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            PersonalHomePageModel *model = [PersonalHomePageModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];
    }
    return _dataArray;
}
-(UIButton*)righBtn{
    if(!_righBtn){
        _righBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_righBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_righBtn setTitle:@"取消" forState:UIControlStateSelected];
        [_righBtn setTitleColor:KBlackColor forState:UIControlStateNormal];
        [_righBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _righBtn;
}

-(UIView *)createBottomView{
    UIView * view = UIView.new;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"一键清空" forState:UIControlStateNormal];
    [leftBtn setTitleColor:UIColorFromRGB(0x454F44) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
    
    kWeakSelf(weakSelf);
    [leftBtn addActionHandler:^{
        [weakSelf deleAction:0];
    }];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"删除" forState:UIControlStateNormal];
    [rightBtn setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SYSTEMFONT(16);

    [rightBtn addActionHandler:^{
        [weakSelf deleAction:1];
    }];

    UIView * lineView = UIView.new;
    lineView.backgroundColor = UIColorFromRGB(0x959E97);
    
    [view addSubview:leftBtn];
    [view addSubview:rightBtn];
    [view addSubview:lineView];

    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.mas_equalTo(0);
        make.right.mas_equalTo(view.mas_centerX).offset(-1);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
        make.width.mas_equalTo(1);
    }];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.mas_equalTo(0);
        make.left.mas_equalTo(view.mas_centerX).offset(1);
    }];

    return view;
}
-(void)deleAction:(NSInteger)seleIndex{
    NSInteger count = 0;

    if (seleIndex == 0) {
        count = self.dataArray.count;
        NSMutableArray * array = [NSMutableArray array];
        for (PersonalHomePageModel * model in self.dataArray) {
            model.isSelecte = YES;
            [array addObject:model];
        }
        self.dataArray = array;
        
    }else if (seleIndex == 1){
        
        for (PersonalHomePageModel * model in self.dataArray) {
            if (model.isSelecte) {
                count ++;
            }
        }
        if (count == 0) {
            ShowHubMessage(@"请选择需要删除的记录");
            return;
        }

    }
    
    kWeakSelf(weakSelf);
    FJAlertController * vc = [FJAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"确认删除%ld条阅读历史吗？",(long)count] preferredStyle:UIAlertControllerStyleAlert];
    FJAlertAction * action1 = [FJAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    FJAlertAction * action2 = [FJAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSMutableArray * array = [NSMutableArray array];
        for (PersonalHomePageModel * model in weakSelf.dataArray) {
            if (!model.isSelecte) {
                [array addObject:model];
            }
        }
        weakSelf.dataArray = array;
        [weakSelf.listView reloadData];
    }];
    
    [vc addAction:action1];
    [vc addAction:action2];
    action1.textColor = UIColorFromRGB(0x009966);
    action2.textColor = UIColorFromRGB(0x009966);

    [self presentViewController:vc animated:YES completion:nil];

}
@end
